package com.example.user.service;

import com.example.common.utils.JwtUtil;
import com.example.user.client.UserFeignClient;
import com.example.user.mapper.LoginInfoMapper;
import com.example.user.model.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class AuthService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Transactional
    public String register(String username, String password) {

        Long userId = userFeignClient.save(username, password);
        LoginInfo loginInfo = new LoginInfo().setUserId(userId).setLoginTime(new Date().getTime());
        loginInfoMapper.insert(loginInfo);
        return JwtUtil.generateToken(username);
    }
}
